package com.example.smartcity.ui.home.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.FragmentNewsListViewAdapter;
import com.example.smartcity.ui.Adapter.initNewsItem;
import com.example.smartcity.ui.Bean.NewsBean;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.example.smartcity.ui.home.News;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link f1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class f1 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private ArrayList<initNewsItem> initList = new ArrayList<>();
    private ListView listView;
    // TODO: Rename and change types of parameters
    private List<NewsBean.RowsBean> rowsBeans;
    public f1() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param beans Parameter 1.
     * @return A new instance of fragment f1.
     */
    // TODO: Rename and change types and number of parameters
    public static f1 newInstance(List<NewsBean.RowsBean> beans) {
        f1 fragment = new f1();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_PARAM1,(Serializable)beans);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_f1, container, false);
        listView = view.findViewById(R.id.NewsList);
       setInitList();
        return view;
    }
    public void setInitList(){
        //清除多加的集合
        initList.clear();
        if (getArguments() != null) {
            rowsBeans = (List<NewsBean.RowsBean>) getArguments().getSerializable(ARG_PARAM1);
            for (int i = 0; i < rowsBeans.size(); i++) {
                initNewsItem item = new initNewsItem(OkHttpUtils.getBaseUrl(getContext())+rowsBeans.get(i).getImgUrl()
                        ,rowsBeans.get(i).getTitle(),rowsBeans.get(i).getContent());
                initList.add(item);
            }
            FragmentNewsListViewAdapter adapter = new FragmentNewsListViewAdapter(getContext(),R.layout.newsitemlayout,initList);
            listView.setAdapter(adapter);
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(f1.this.getContext(), News.class);
                if (getArguments()!=null){
                    rowsBeans = (List<NewsBean.RowsBean>) getArguments().getSerializable(ARG_PARAM1);
                    String title = rowsBeans.get(position).getTitle();
                    String imageUrl = OkHttpUtils.getBaseUrl(getContext())+rowsBeans.get(position).getImgUrl();
                    String Content = rowsBeans.get(position).getContent();
                    intent.putExtra("title",title);
                    intent.putExtra("imgUrl",imageUrl);
                    intent.putExtra("Content",Content);
                }
                startActivity(intent);
            }
        });
    }
}